﻿using System;
using System.Collections.Generic;
using WordContentControls = Microsoft.Office.Interop.Word.ContentControls;
using WordContentControl = Microsoft.Office.Interop.Word.ContentControl;
using WordDocument = Microsoft.Office.Interop.Word.Document;

namespace CodeCounsel.Office.DatabindingToolkit
{
    public class ContentControlCollection
    {
        WordContentControls _wordContentControls = null;
        List<ContentControl> _contentControls = null;

        public ContentControl this[string id]
        {
            get
            {
                return _contentControls.Find(
                    delegate(ContentControl searchItem)
                    {
                        return searchItem.ID == id;
                    });
            }
        }

        public ContentControlCollection(WordDocument wordDocument,
            WordContentControls wordContentControls)
        {
            _wordContentControls = wordContentControls;
            _contentControls = new List<ContentControl>();
            foreach (WordContentControl wordContentControl in wordContentControls)
            {
                AddContentControl(wordContentControl);
            }
            wordDocument.ContentControlAfterAdd += new Microsoft.Office.Interop.Word.DocumentEvents2_ContentControlAfterAddEventHandler(WordDocument_ContentControlAfterAdd);
        }

        void AddContentControl(WordContentControl wordContentControl)
        {
            _contentControls.Add(new ContentControl(wordContentControl));
        }

        void WordDocument_ContentControlAfterAdd(
            WordContentControl wordContentControl, bool InUndoRedo)
        {
            AddContentControl(wordContentControl);
        }
    }

}
